package com.spc.car.web.log.module;

import com.spc.car.common.utils.spring.SpringUtils;
import com.spc.car.core.framework.web.domain.AjaxResult;
import com.spc.car.data.domain.project.po.ProjectTenderingProjectPO;
import com.spc.car.data.entity.log.LogProjectOperation;
import com.spc.car.data.entity.project.ProjectTendering;
import com.spc.car.data.service.project.ProjectTenderingService;
import com.spc.car.web.log.ProjectAction;
import com.spc.car.web.log.annotation.LogProjectAction;
import org.apache.commons.lang3.StringUtils;

import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

public class LogProjectTendering extends BaseLogModule<LogProjectOperation, LogProjectAction> {
    public ProjectTenderingService tenderingService;

    public LogProjectTendering() {
        this.tenderingService = SpringUtils.getBean(ProjectTenderingService.class);
    }

    public LogProjectOperation toLogRecord(LogProjectAction logProjectAction,
                                           Map<String, Object> paramsMap,
                                           AjaxResult result) {
        LogProjectOperation logProjectOperation = new LogProjectOperation();

        ProjectAction[] projectActions = logProjectAction.action();
        ProjectTendering projectTendering = null;
        String projectTenderingCode = null;
        if (StringUtils.isNotBlank(logProjectAction.idForPost())
                && Objects.nonNull(paramsMap.get(logProjectAction.idForPost()))) {
            if (paramsMap.get(logProjectAction.idForPost()) instanceof Integer) {
                projectTendering = tenderingService.getById((Integer) paramsMap.get(logProjectAction.idForPost()));
            } else if (paramsMap.get(logProjectAction.idForPost()) instanceof String) {
                projectTenderingCode = (String)paramsMap.get(logProjectAction.idForPost());
            }
        }

        for (ProjectAction projectAction: projectActions) {
            if (projectAction.equals(ProjectAction.ADD_TENDERING)
                    && null == projectTendering
                    && StringUtils.isNotBlank(logProjectAction.idForReturn())
                    && Objects.nonNull(result.getByKey(logProjectAction.idForReturn()))) {
                Object idObj =  result.getByKey(logProjectAction.idForReturn());
                if (idObj instanceof String) {
                    logProjectOperation.setProjectTenderingCode((String)idObj);
                    logProjectOperation.setRemark(projectAction.getName()
                            + "[" + paramsMap.get("projectName") + "]");
                    break;
                }
            } else if (projectAction.equals(ProjectAction.UPDATE_TENDERING) && null != projectTendering) {
                logProjectOperation.setProjectTenderingCode(projectTendering.getCode());
                logProjectOperation.setRemark(projectAction.getName()
                        + "[" + StringUtils.defaultIfBlank(
                                null == paramsMap.get("projectName") ? "": (String)paramsMap.get("projectName"),
                                projectTendering.getProjectName()) + "]");
                break;
            } else if (projectAction.equals(ProjectAction.CREATE_RELATION)
                    && StringUtils.isNotBlank(projectTenderingCode)) {
                Object obj = paramsMap.get("projectList");
                if (Objects.nonNull(obj) && obj instanceof List ) {
                    logProjectOperation.setProjectTenderingCode(projectTenderingCode);
                    List<ProjectTenderingProjectPO> relationPOList = (List)paramsMap.get("projectList");
                    String projectNames = StringUtils.join(relationPOList.stream()
                            .map(ProjectTenderingProjectPO::getProjectName)
                            .distinct().collect(Collectors.toList()), ",");

                    logProjectOperation.setRemark(projectAction.getName()
                            + "[" + projectNames + "]");
                }
                break;
            } else if (projectAction.equals(ProjectAction.CANCEL_RELATION)) {
                String tenderingCode = (String)paramsMap.get("projectTenderingCode");
                String projectName = (String)paramsMap.get("projectName");

                if (Objects.nonNull(tenderingCode) && StringUtils.isNotBlank(projectName)) {
                    logProjectOperation.setProjectTenderingCode(tenderingCode);
                    logProjectOperation.setRemark(projectAction.getName()
                            + "[" + projectName + "]");
                }
                break;
            }
        }

        return logProjectOperation;
    }
}
